import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'base_model.dart';

/// created by hcl
/// on 2023/3/6
/// page base_page
/// desc
abstract class BaseWidgetState<T extends BaseModel, P extends StatefulWidget>
    extends State<P> with AutomaticKeepAliveClientMixin {
  @override
  @protected
  bool get wantKeepAlive => false;

  final FocusNode _blankNode = FocusNode();

  late T model;

  T initModel();

  @protected
  Widget bodyWidget(BuildContext context);

  @override
  void initState() {
    model = initModel();
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((callback) {
      model.mContext = context;
      model.initData();
    });
  }

  void hideKeyboard() {
    FocusScope.of(context).requestFocus(_blankNode);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ChangeNotifierProvider<T>(
      create: (context) => model,
      child: bodyWidget(context),
    );
  }
}
